<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 16进制：0-9 a-f
      // 16进制颜色：#000000-#ffffff
      let arr = [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
      ];
      //  rgb(0-255,0-255,0-255)
      // 获取0-255之间的随机数：Math.floor(Math.random()*256)
      function getColor(flag) {
        if (flag) {
          // 返回rgb
          let r = Math.floor(Math.random() * 256);
          let g = Math.floor(Math.random() * 256);
          let b = Math.floor(Math.random() * 256);
          return `rgb(${r},${g},${b})`;
        } else {
          // 返回16进制
          let color = "#";
          for (let i = 0; i < 6; i++) {
            color += arr[Math.floor(Math.random() * arr.length)];
          }
          return color;
        }
      }

      console.log(getColor(false));
    </script>
  </body>
</html>
